package com.xdwanj.suffix;

import org.w3c.dom.events.Event;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Model extends JFrame {

    private JTextField tag_1;
    private JTextField tag_2;
    public JTextField input_1;
    public JTextField input_2;
    public JButton bn;
    private String beforeChange; //承接未改的后缀
    private String overChange; //承接已改的后缀
    private static String textBat; //形成已经可以执行的dos命令
    private PrintStream psOut = null; //输出流

    public Model() {
        launch();
    }

    public void launch() {
        this.setTitle("王杰的后缀编辑器");
        this.setSize(340, 300);
        this.setResizable(false);
        this.setLocation(500, 200);

        Container container = this.getContentPane();

        tag_1 = new JTextField("改前");
        tag_1.setBounds(50, 50, 100, 30);
        tag_1.setHorizontalAlignment(SwingConstants.CENTER);

        tag_2 = new JTextField("改后");
        tag_2.setBounds(180, 50, 100, 30);
        tag_2.setHorizontalAlignment(SwingConstants.CENTER);

        input_1 = new JTextField();
        input_1.setBounds(50, 110, 100, 30);
//        input_1.addActionListener(new MyMonitorBefore());
        input_1.getDocument().addDocumentListener(new MyMonitorBefore());
        input_1.setHorizontalAlignment(SwingConstants.CENTER);

        input_2 = new JTextField();
        input_2.setBounds(180, 110, 100, 30);
//        input_2.addActionListener(new MyMonitorOver());
        input_2.getDocument().addDocumentListener(new MyMonitorOver());
        input_2.setHorizontalAlignment(SwingConstants.CENTER);

        bn = new JButton("start");
        bn.setBounds(50, 170, 230, 50);
        bn.addActionListener(new MyMonitorButton());

        container.add(tag_1);
        container.add(tag_2);
        container.add(input_1);
        container.add(input_2);
        container.add(bn);

        this.setLayout(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    class MyMonitorBefore implements ActionListener, DocumentListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField tf_temp = (JTextField) e.getSource();
            beforeChange = tf_temp.getText();
            System.out.println(tf_temp.getParent());
//            System.out.println(beforeChange);

        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            Document Do = e.getDocument();

            try {
                beforeChange = Do.getText(0, Do.getLength());
            } catch (BadLocationException badLocationException) {
//                badLocationException.printStackTrace();
            }
//            System.out.println(beforeChange);

        }

        @Override
        public void removeUpdate(DocumentEvent e) {

        }

        @Override
        public void changedUpdate(DocumentEvent e) {

        }
    }

    class MyMonitorOver implements ActionListener, DocumentListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField tf_temp = (JTextField) e.getSource();
            overChange = tf_temp.getText();
//            System.out.println(overChange);
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            Document Do = e.getDocument();

            try {
                overChange = Do.getText(0, Do.getLength());
            } catch (BadLocationException badLocationException) {
//                badLocationException.printStackTrace();
            }
//            System.out.println(overChange);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {

        }

        @Override
        public void changedUpdate(DocumentEvent e) {

        }
    }

    class MyMonitorButton implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            textBat = "ren *." + beforeChange + " *." + overChange;
//            System.out.println(textBat);
            try {
                psOut = new PrintStream("1.bat");

                System.setOut(psOut);
                System.out.println(textBat);
                psOut.close();
                new Cmd().CmdStart();
                new Delete().DeleteKey();
            } catch (FileNotFoundException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
//                System.setErr(psErr);
            } finally {
//                psErr.close();
            }
        }
    }

    class Cmd {
        public void CmdStart() {
            Process process = null;
            try {
                process = Runtime.getRuntime().exec("1.bat");
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    class Delete {
        public void DeleteKey() {
            try {
                File file = new File("1.bat");
                file.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
